<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<h1 id="show"></h1>
<input type="text" id="txt">

<body>
    <!-- 手写vmodel -->
    <script>
        var show = document.getElementById("show")
        var data = {
            name: ""
        }
        show.innerHTML = data.name;

        var txt = document.getElementById("txt");
        var str = ""
        txt.oninput = function() {
            var txtval = this.value;
            data.name = txtval;

            Object.defineProperty(data, 'name', {
                // value: "小王",
                // configurable: true, //能否被删除
                // enumerable: true, //能否被遍历
                // writable: true, //能否被修改
                get() {
                    return str
                },
                set(val) {
                    str = val;
                    show.innerHTML = data.name;
                }
            })
        }
    </script>
</body>

</html>